﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportService2Ref;
    using WMS6.Web.UserControls;

    public class RentWhDetailRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        private string currFlagOwner = string.Empty;
        protected HtmlForm F15691;
        protected GridView gvRentWhDetail;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnPara;
        protected HtmlInputButton htmlbtnReport;
        private string lastFlagOwner = string.Empty;
        protected Label lblInvoiceList;
        protected Label lblMsg;
        protected LinkButton lnkbtnPrintRentWh;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected HtmlTableCell tdlnkbtnPrintRentWh;
        protected UpdatePanel up1;

        private void BindData2RentWhDetailList(RentWhDetailRptQueryEntity rentWhDetailRptQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(rentWhDetailRptQueryEntity.CurrentPage);
            DataSet ds = AnalyticsReportService2ClientProxy.GetInstance().GetRentWhDetailRptDataSet(rentWhDetailRptQueryEntity);
            this.gvRentWhDetail.Visible = true;
            base.BindData2GridView(this.gvRentWhDetail, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            RentWhDetailRptQueryEntity rentWhDetailRptQueryEntity = this.GetRentWhDetailQueryEntity();
            rentWhDetailRptQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = rentWhDetailRptQueryEntity.CurrentPage;
            rentWhDetailRptQueryEntity.CurrentPage = "0";
            try
            {
                using (DataSet ds = AnalyticsReportService2ClientProxy.GetInstance().GetRentWhDetailRptDataSet(rentWhDetailRptQueryEntity))
                {
                    base.Export2Excel("RentWhDetailRpt.xls", "15691", this.gvRentWhDetail, this.SetExportData(ds));
                }
            }
            finally
            {
                rentWhDetailRptQueryEntity.IsGetAll = false;
                rentWhDetailRptQueryEntity.CurrentPage = currentPageIndex;
                this.SetRentWhDetailQueryEntity(rentWhDetailRptQueryEntity);
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            RentWhDetailRptQueryEntity rentWhDetailRptQueryEntity = this.GetRentWhDetailQueryEntity();
            rentWhDetailRptQueryEntity.CurrentPage = "0";
            this.BindData2RentWhDetailList(rentWhDetailRptQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            RentWhDetailRptQueryEntity rentWhDetailRptQueryEntity = this.GetRentWhDetailQueryEntity();
            rentWhDetailRptQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2RentWhDetailList(rentWhDetailRptQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            RentWhDetailRptQueryEntity rentWhDetailRptQueryEntity = this.GetRentWhDetailQueryEntity();
            rentWhDetailRptQueryEntity.DEPARTMENT_ID = "";
            rentWhDetailRptQueryEntity.OWNER_ID = "";
            rentWhDetailRptQueryEntity.ZONE_NO = "";
            rentWhDetailRptQueryEntity.MANGAGE_TYPE = "";
            rentWhDetailRptQueryEntity.StartDateTime = "";
            rentWhDetailRptQueryEntity.EndDateTime = "";
            this.BindData2RentWhDetailList(rentWhDetailRptQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            RentWhDetailRptQueryEntity rentWhDetailRptQueryEntity = this.GetRentWhDetailQueryEntity();
            if (int.Parse(rentWhDetailRptQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                rentWhDetailRptQueryEntity.CurrentPage = (int.Parse(rentWhDetailRptQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2RentWhDetailList(rentWhDetailRptQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            RentWhDetailRptQueryEntity rentWhDetailRptQueryEntity = this.GetRentWhDetailQueryEntity();
            if (int.Parse(rentWhDetailRptQueryEntity.CurrentPage) > 0)
            {
                rentWhDetailRptQueryEntity.CurrentPage = (int.Parse(rentWhDetailRptQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2RentWhDetailList(rentWhDetailRptQueryEntity);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            RentWhDetailRptQueryEntity rentWhDetailRptQueryEntity = this.GetRentWhDetailQueryEntity();
            this.BindData2RentWhDetailList(rentWhDetailRptQueryEntity);
        }

        private DataSet GetPrintDataset()
        {
            DataSet ds = null;
            RentWhDetailRptQueryEntity rentWhDetailRptQueryEntity = this.GetRentWhDetailQueryEntity();
            rentWhDetailRptQueryEntity.IsGetAll = true;
            string currentPageIndex = rentWhDetailRptQueryEntity.CurrentPage;
            rentWhDetailRptQueryEntity.CurrentPage = "0";
            try
            {
                ds = this.SetPrintData(AnalyticsReportService2ClientProxy.GetInstance().GetRentWhDetailRptDataSet(rentWhDetailRptQueryEntity));
            }
            finally
            {
                rentWhDetailRptQueryEntity.IsGetAll = false;
                rentWhDetailRptQueryEntity.CurrentPage = currentPageIndex;
                this.SetRentWhDetailQueryEntity(rentWhDetailRptQueryEntity);
            }
            return ds;
        }

        private RentWhDetailRptQueryEntity GetRentWhDetailQueryEntity()
        {
            return (base.PageQueryEntitySession as RentWhDetailRptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                RentWhDetailRptQueryEntity rentWhDetailRptQueryEntity = this.GetRentWhDetailQueryEntity();
                rentWhDetailRptQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2RentWhDetailList(rentWhDetailRptQueryEntity);
            }
        }

        protected void gvRentWhDetail_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15691", this.gvRentWhDetail);
        }

        protected void gvRentWhDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                if (this.hdnActionID.Value != "EXPORT")
                {
                }
                this.currFlagOwner = ((Label) e.Row.FindControl("lblOwnerID")).Text;
                if (!string.IsNullOrEmpty(this.currFlagOwner))
                {
                    if (this.currFlagOwner.ToUpper().Trim() == this.lastFlagOwner.ToUpper().Trim())
                    {
                        ((Label) e.Row.FindControl("lblOwnerID")).Text = "";
                    }
                    this.lastFlagOwner = this.currFlagOwner;
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
            }
        }

        protected void lnkbtnPrintRentWh_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = this.GetPrintDataset();
                if ((ds != null) && (ds.Tables[0].Rows.Count > 0))
                {
                    this.Session["PrintRentWhDetail"] = ds;
                    RentWhDetailRptQueryEntity rentWhDetailRptQueryEntity = this.GetRentWhDetailQueryEntity();
                    string date = string.IsNullOrEmpty(rentWhDetailRptQueryEntity.StartDateTime) ? "" : Convert.ToDateTime(rentWhDetailRptQueryEntity.StartDateTime).ToString("yyyy-MM-dd");
                    string para = date + " 至 " + (string.IsNullOrEmpty(rentWhDetailRptQueryEntity.EndDateTime) ? "" : Convert.ToDateTime(rentWhDetailRptQueryEntity.EndDateTime).ToString("yyyy-MM-dd"));
                    this.hdnPara.Value = para;
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>popPrintRentWhDetail();</script>", false);
                }
            }
            catch
            {
            }
        }

        private void LoadData()
        {
            RentWhDetailRptQueryEntity rentWhDetailRptQueryEntity = new RentWhDetailRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    rentWhDetailRptQueryEntity.DEPARTMENT_ID = this.PreviousPage.DepartmentID;
                    rentWhDetailRptQueryEntity.OWNER_ID = this.PreviousPage.OwnerID;
                    rentWhDetailRptQueryEntity.ZONE_NO = this.PreviousPage.ZoneNO;
                    rentWhDetailRptQueryEntity.MANGAGE_TYPE = this.PreviousPage.ManageType;
                    rentWhDetailRptQueryEntity.StartDateTime = this.PreviousPage.StartDate;
                    rentWhDetailRptQueryEntity.EndDateTime = this.PreviousPage.EndDate;
                }
                else
                {
                    rentWhDetailRptQueryEntity.DEPARTMENT_ID = "";
                    rentWhDetailRptQueryEntity.OWNER_ID = "";
                    rentWhDetailRptQueryEntity.ZONE_NO = "";
                    rentWhDetailRptQueryEntity.MANGAGE_TYPE = "";
                    rentWhDetailRptQueryEntity.StartDateTime = "";
                    rentWhDetailRptQueryEntity.EndDateTime = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            rentWhDetailRptQueryEntity.IsGetAll = false;
            rentWhDetailRptQueryEntity.CurrentPage = "0";
            rentWhDetailRptQueryEntity.PageSize = this.gvRentWhDetail.PageSize.ToString();
            rentWhDetailRptQueryEntity.SortField = " D.DEPARTMENT_ID,O.OWNER_ID";
            rentWhDetailRptQueryEntity.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvRentWhDetail.PageSize;
            this.SetRentWhDetailQueryEntity(rentWhDetailRptQueryEntity);
            this.BindData2RentWhDetailList(rentWhDetailRptQueryEntity);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvRentWhDetail);
                this.LoadData();
            }
        }

        private DataSet SetExportData(DataSet ds)
        {
            string currFlagOwner = string.Empty;
            string lastFlagOwner = string.Empty;
            try
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    currFlagOwner = ds.Tables[0].Rows[i]["OWNER_ID"].ToString();
                    if (currFlagOwner.ToUpper().Trim() == lastFlagOwner.ToUpper().Trim())
                    {
                        ds.Tables[0].Rows[i]["OWNER_ID"] = "";
                        ds.Tables[0].Rows[i]["OWNER_D"] = "";
                    }
                    lastFlagOwner = currFlagOwner;
                }
            }
            catch
            {
                return null;
            }
            return ds;
        }

        private DataSet SetPrintData(DataSet ds)
        {
            string currFlagOwner = string.Empty;
            string lastFlagOwner = string.Empty;
            try
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    currFlagOwner = ds.Tables[0].Rows[i]["OWNER_ID"].ToString() + ds.Tables[0].Rows[i]["DEPARTMENT_ID"].ToString();
                    if (currFlagOwner.ToUpper().Trim() == lastFlagOwner.ToUpper().Trim())
                    {
                        ds.Tables[0].Rows[i]["OWNER_ID"] = "";
                        ds.Tables[0].Rows[i]["OWNER_D"] = "";
                    }
                    lastFlagOwner = currFlagOwner;
                }
            }
            catch
            {
                return null;
            }
            return ds;
        }

        private void SetRentWhDetailQueryEntity(RentWhDetailRptQueryEntity rentWhDetailRptQueryEntity)
        {
            base.PageQueryEntitySession = rentWhDetailRptQueryEntity;
        }

        public RentWhDetailRptQuery PreviousPage
        {
            get
            {
                return (RentWhDetailRptQuery) base.PreviousPage;
            }
        }
    }
}

